#include "PayBox.h"
#include <Arduino.h>
#include <OneButton.h>
#include <SD.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <WiFi.h>

TFT_eSPI tft = TFT_eSPI();
char buf[32] = {0};
BluetoothA2DPSink a2dp_sink;
OneButton buttons[] = {OneButton(PAY_KEY_ON), OneButton(PAY_KEY_ADD),
                       OneButton(PAY_KEY_MINUS), OneButton(PAY_KEY_REFRESH)};
unsigned long s1Tick = 0;   // 1秒计时
unsigned long ms10Tick = 0; // 10毫秒计时
uint8_t volume = 50;
bool isPause = false;

void inline setVolume(uint8_t v) {
  volume = v;
  a2dp_sink.set_volume(volume);
  sprintf(buf, "Volume: %02d", volume / 10);
  tft.setTextColor(TFT_CYAN, TFT_BLACK);
  tft.drawCentreString(buf, 160, 100, 4);
  tft.fillRoundRect(60, 140, 200, 8, 2, TFT_BLACK);
  tft.drawRoundRect(60, 140, 200, 8, 2, TFT_PINK);
  int pros = v * 200 / 120;
  tft.fillRoundRect(60, 140, pros, 8, 2, TFT_PINK);
}

void inline setupAudioPlay() {
  setVolume(50);
  const i2s_pin_config_t pc = {.bck_io_num = PAY_AUDIO_SCLK,
                               .ws_io_num = PAY_AUDIO_LRCK,
                               .data_out_num = PAY_AUDIO_SDATA,
                               .data_in_num = I2S_PIN_NO_CHANGE};
  a2dp_sink.set_pin_config(pc);
  a2dp_sink.start("PayBox A2", true);
  tft.setTextColor(TFT_BROWN, TFT_BLACK);
  tft.drawCentreString("Bluetooth Speaker", 160, 60, 4);
  a2dp_sink.set_on_connected2BT([] {
    tft.fillRect(0, 160, 320, 26, TFT_BLACK);
    tft.setTextColor(TFT_GREEN, TFT_BLACK);
    tft.drawCentreString("BT Connected!", 160, 160, 4);
  });
  a2dp_sink.set_on_disconnected2BT([] {
    tft.fillRect(0, 160, 320, 26, TFT_BLACK);
    tft.setTextColor(TFT_RED, TFT_BLACK);
    tft.drawCentreString("BT Disconnected!", 160, 160, 4);
  });
  payAudioInit();
}

void inline setupButtonEvents() {
  // reset
  buttons[0].attachClick([] {
    payAudioDisable();
    ESP.restart();
  });
  // up
  buttons[1].attachClick([] { a2dp_sink.next(); });
  buttons[1].attachDoubleClick([] {
    if (volume < 120) {
      setVolume(volume + 10);
    }
  });
  // down
  buttons[2].attachClick([] { a2dp_sink.previous(); });
  buttons[2].attachDoubleClick([] {
    if (volume > 10) {
      setVolume(volume - 10);
    }
  });
  buttons[3].attachClick([] {
    if (isPause) {
      a2dp_sink.play();
    } else {
      a2dp_sink.pause();
    }
    isPause = !isPause;
  });
}

void setup() {
  Serial.begin(115200);
  payTFTInit(tft, 32);
  payLEDInit();
  payLEDShow(0xf6229a);
  tft.println("Start Wifi Connect!");
  autoConfigWifi();
  tft.println("Wifi Connected!");
  sprintf(buf, "IP: %s", WiFi.localIP().toString().c_str());
  tft.println(buf);
  setupOTAConfig();
  delay(1000);
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.drawCentreString(buf, 160, 220, 2);
  setupAudioPlay();
  setupButtonEvents();
}

void loop() {
  unsigned long cMs = millis();
  if (cMs - s1Tick >= 1000) {
    s1Tick = cMs;
    ArduinoOTA.handle();
    auto led = random(0x010101, 0xffffff);
    payLEDShow(led);
  }
  if (cMs - ms10Tick >= 10) {
    ms10Tick = cMs;
    for (int i = 0; i < 4; i++) {
      buttons[i].tick();
    }
  }
}